//
//  SXRefreashControl.swift
//  微博09项目
//
//  Created by mac on 15/9/13.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

private let SXRefreshControlMaxOffset: CGFloat = -60
class SXRefreashControl: UIRefreshControl {
  
    //停止刷新
    override func endRefreshing()
        {
            //一定要调用父类的方法
            super.endRefreshing()
            self.refreshView.stopAnimation()
        }
    
    override init() {
        super.init()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init()
        setupUI()
    }
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if frame.origin.y > 0{
            return
        }
        //判断是否正在刷新
        if refreshing {
            refreshView.loadingAnimation()
            return
        }
        if frame.origin.y < SXRefreshControlMaxOffset && !refreshView.rotateFlag{
            refreshView.rotateFlag = true
        }else if frame.origin.y >= SXRefreshControlMaxOffset && refreshView.rotateFlag
        {
            refreshView.rotateFlag = false
        }
       // print(frame)
    }
    
    //移除监听
    deinit{
        self.removeObserver(self, forKeyPath: "frame")
    }
   
    private func setupUI(){
        //通过KVO监听 self 监听self.frame
        self.addObserver(self, forKeyPath: "frame", options: [], context: nil)
       tintColor = UIColor.clearColor()
        addSubview(refreshView)
        refreshView.ff_AlignInner(type: ff_AlignType.TopCenter, referView: self, size:refreshView.bounds.size)
    }
    private lazy var refreshView = SXRefreshView.refreshView()
}

class SXRefreshView: UIView  {
   
  private var rotateFlag = false{
        didSet{
            rotateTipAnimation()
        }
    }
    //加载图标
    @IBOutlet weak var loadingIcon: UIImageView!
    //提示视图
    @IBOutlet weak var tipVIew: UIView!
    //下拉视图
    @IBOutlet weak var pullDownTipIcon: UIImageView!
    
    
    class func refreshView() -> SXRefreshView{
        return NSBundle.mainBundle().loadNibNamed("RefreshVIew", owner: nil, options: nil).last! as! SXRefreshView
    }
    
    //设置箭头
    private func rotateTipAnimation(){
        var angle = CGFloat(M_PI)
        angle += rotateFlag ? -0.01 : 0.01
        UIView.animateWithDuration(0.5) { () -> Void in
            self.pullDownTipIcon.transform = CGAffineTransformRotate(self.pullDownTipIcon.transform, angle)
        }
    }
    
    //加载动画
    private func loadingAnimation(){
        let loadingKey = "loadingKey"
        if loadingIcon.layer.animationForKey(loadingKey) != nil{
            return
        }
        tipVIew.hidden = true
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.duration = 1.0
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
       loadingIcon.layer.addAnimation(anim, forKey: loadingKey)
    }
    private func stopAnimation(){
        tipVIew.hidden = false
        loadingIcon.layer.removeAllAnimations()
    }

}
